﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace Barockoco_Chat_Server
{
    public partial class Form1 : Form
    {
        private delegate void UpdateStatusCallback(string strMessage);
        public Form1()
        {
            InitializeComponent();

            Array ComputerIP = Dns.GetHostAddresses(Dns.GetHostName());//braucht man nur auf phenylls pc, sonst standard wert wie unten
            string OutPut = "";
            OutPut += "192.168.178.100"; //nur auf phenylls pc.. ComputerIP.GetValue(3); // hole alle ipconfig infos und schreibe die dritte zeile in Output
            this.txtIP.Text = OutPut; // Nach ip finden, OutPut ins textfeld für die ip schreiben
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            if (ChatServer.ServRunning == false)
            {
                // Parse the server's IP address out of the TextBox
                IPAddress ipAddr = IPAddress.Parse(txtIP.Text);
                // Create a new instance of the ChatServer object
                ChatServer mainServer = new ChatServer(ipAddr);
                // Hook the StatusChanged event handler to mainServer_StatusChanged
                ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
                // Start listening for connections
                mainServer.StartListening();
                // Show that we started to listen for connections
                txtLog.AppendText("Monitoring for connections...\r\n");
                btnListen.Text = "Stop";
            }
            else
            {
                //ChatServer.StopListening(IPAddress);
                
                // try: mainServer_StatusChanged --> will change status and posibly turn off the server???

                MessageBox.Show("Hier noch Fehler, Chatserver kann nicht beendet werden - Bitte wenden Sie sich an den Hersteller der Software.");
                //btnListen.Text = "Start";
            }
        }
        
        public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Call the method that updates the form
            this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }

        private void UpdateStatus(string strMessage)
        {
            // Updates the log with the message
            txtLog.AppendText(strMessage + "\r\n");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
